﻿Option Explicit On
Option Strict On

Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports FRAMEWORK
Imports System.Drawing.Drawing2D
Public Class frm_UserPermission
    Inherits asWins.Forms.BaseForm

    'Global Variable
    Dim DA As New FRAMEWORK.SQLDataAccess
    Dim User_ID As Integer

#Region " Function and Procedure "
    'Private Sub Handler()
    '    ControlGotFocus(Me, Me)
    '    ControlLostFocus(Me, Me)
    'End Sub
    'Private Sub ControlGotFocus(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
    '    Dim ctr As Control
    '    For Each ctr In parentCtr.Controls
    '        If Not (ctr Is ignoreControl) Then
    '            If TypeOf ctr Is TextBox Then
    '                AddHandler ctr.GotFocus, AddressOf EvenHand.KeyEventHand.TextBox_GotFocus
    '            ElseIf TypeOf ctr Is ComboBox Then
    '                AddHandler DirectCast(ctr, ComboBox).GotFocus, AddressOf EvenHand.KeyEventHand.ComboBox_GotFocus
    '            End If
    '        End If
    '        ControlGotFocus(ctr, ignoreControl)
    '    Next
    'End Sub
    'Private Sub ControlLostFocus(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
    '    Dim ctr As Control
    '    For Each ctr In parentCtr.Controls
    '        If Not (ctr Is ignoreControl) Then
    '            If TypeOf ctr Is TextBox Then
    '                AddHandler ctr.LostFocus, AddressOf EvenHand.KeyEventHand.TextBox_LostFocus
    '            ElseIf TypeOf ctr Is ComboBox Then
    '                AddHandler DirectCast(ctr, ComboBox).LostFocus, AddressOf EvenHand.KeyEventHand.ComboBox_LostFocus
    '            End If
    '        End If
    '        ControlLostFocus(ctr, ignoreControl)
    '    Next
    'End Sub

    'Private Sub Get_ALL_Employee()
    '    Dim dt As DataTable

    '    Dim para(13) As SqlParameter

    '    para(0) = New SqlParameter("@ACTION", 3)
    '    para(1) = New SqlParameter("@Employee_Code", "")
    '    para(2) = New SqlParameter("@Department_ID", 0)
    '    para(3) = New SqlParameter("@Employee_Title", "")
    '    para(4) = New SqlParameter("@Employee_Fname", "")
    '    para(5) = New SqlParameter("@Employee_Lname", "")
    '    para(6) = New SqlParameter("@Employee_Position", "")
    '    para(7) = New SqlParameter("@Employee_Sale", 0)
    '    para(8) = New SqlParameter("@Employee_StatusAccount", 0)
    '    para(9) = New SqlParameter("@Employee_level", 0)
    '    para(10) = New SqlParameter("@Employee_Status", 0)
    '    para(11) = New SqlParameter("@Create_User", "")
    '    para(12) = New SqlParameter("@Update_User", "")
    '    dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_EMPLOYEE", para).Tables(0)

    '    If dt.Rows.Count > 0 Then
    '        dgvDetails.DataSource = dt
    '        FormatHeader()
    '    Else
    '        dgvDetails.DataSource = Nothing
    '    End If


    'End Sub
    'Private Sub FormatHeader()
    '    With dgvDetails
    '        If .RowCount > 0 Then
    '            .Columns(0).HeaderText = "รหัส"
    '            .Columns(1).HeaderText = "คำนำหน้า"
    '            .Columns(2).HeaderText = "ชื่อ"
    '            .Columns(3).HeaderText = "นามสกุล"
    '            .Columns(4).HeaderText = "หน้าที่/ตำแหน่ง"
    '            .Columns(5).HeaderText = "แผนก"
    '            .Columns(6).HeaderText = "Sales"
    '            .Columns(7).HeaderText = "User Account"
    '            .Columns(8).HeaderText = "Level"

    '            .Columns(0).Width = 100
    '            .Columns(1).Width = 80 '.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    '            .Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    '            .Columns(3).Width = 100
    '            .Columns(4).Width = 100
    '            .Columns(5).Width = 100
    '            .Columns(6).Width = 100
    '            .Columns(7).Width = 100
    '            .Columns(8).Width = 100
    '        End If
    '    End With
    'End Sub
    'Private Sub ClearData(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
    '    Dim ctr As Control
    '    For Each ctr In parentCtr.Controls
    '        If Not (ctr Is ignoreControl) Then
    '            If TypeOf ctr Is TextBox Then
    '                ctr.Text = ""
    '            ElseIf TypeOf ctr Is ComboBox Then
    '                DirectCast(ctr, ComboBox).SelectedIndex = -1
    '            ElseIf TypeOf ctr Is ListBox Then
    '                CType(ctr, ListBox).Items.Clear()
    '            End If
    '        End If
    '        ClearData(ctr, ignoreControl)
    '    Next
    '    chkSales.Checked = False
    'End Sub

    'Private Sub Execute_Data()
    '    If cboDepart.SelectedIndex = -1 Then
    '        MessageBox.Show("กรุณาป้อนชื่อแผนก !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    '        cboDepart.Focus()
    '        Exit Sub
    '    End If
    '    If txtTitle.Text.Trim() = "" Then
    '        MessageBox.Show("กรุณาป้อนคำนำหน้าก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    '        txtTitle.Focus()
    '        Exit Sub
    '    End If
    '    If txtFirstName.Text.Trim() = "" Then
    '        MessageBox.Show("กรุณาป้อนชื่อก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    '        txtFirstName.Focus()
    '        Exit Sub
    '    End If
    '    If txtLastName.Text.Trim() = "" Then
    '        MessageBox.Show("กรุณาป้อนนามสกุลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    '        txtLastName.Focus()
    '        Exit Sub
    '    End If

    '    Dim L_complete As Integer = 0
    '    If MessageBox.Show("คุณต้องการเพิ่มข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    '        Try
    '            DA.BeginTransaction()
    '            Dim para(13) As SqlParameter

    '            para(0) = New SqlParameter("@ACTION", 1)
    '            para(1) = New SqlParameter("@Employee_Code", "")
    '            para(2) = New SqlParameter("@Department_ID", cboDepart.SelectedValue)
    '            para(3) = New SqlParameter("@Employee_Title", txtTitle.Text)
    '            para(4) = New SqlParameter("@Employee_Fname", txtFirstName.Text)
    '            para(5) = New SqlParameter("@Employee_Lname", txtLastName.Text)
    '            para(6) = New SqlParameter("@Employee_Position", txtPosition.Text)
    '            If chkSales.Checked Then
    '                para(7) = New SqlParameter("@Employee_Sale", 1)
    '            Else
    '                para(7) = New SqlParameter("@Employee_Sale", 0)
    '            End If
    '            para(8) = New SqlParameter("@Employee_StatusAccount", 0)
    '            para(9) = New SqlParameter("@Employee_level", 0)
    '            para(10) = New SqlParameter("@Employee_Status", 0)
    '            para(11) = New SqlParameter("@Create_User", FRAMEWORK.Framework.F_Username_Str)
    '            para(12) = New SqlParameter("@Update_User", "")
    '            L_complete = DA.ExecuteNonQuery(DA.Transaction, CommandType.StoredProcedure, "sp_SYS_EMPLOYEE", para)
    '            If L_complete > 0 Then
    '                DA.CommitTransaction()
    '                MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '                Call ClearData(Me, Me)
    '                Call Get_ALL_Employee()
    '            Else
    '                DA.RollBackTransaction()
    '                MessageBox.Show("เกิดข้อผิดพลาด", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '                Exit Sub
    '            End If

    '        Catch ex As Exception
    '            DA.RollBackTransaction()
    '            MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '            Exit Sub
    '        End Try

    '        cboDepart.Focus()

    '    Else
    '        Exit Sub
    '    End If
    'End Sub
    'Private Sub Update_Data(ByVal index As Integer, ByVal tmp As String)
    '    If txtCode.Text.Trim() = "" Then
    '        MessageBox.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    '        txtCode.Focus()
    '        Exit Sub
    '    End If
    '    If txtFirstName.Text.Trim() = "" Then
    '        MessageBox.Show("กรุณาป้อนชื่อก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    '        txtFirstName.Focus()
    '        Exit Sub
    '    End If

    '    Dim L_complete As Integer = 0
    '    If MessageBox.Show("คุณต้องการ" & tmp & "ข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    '        Try
    '            DA.BeginTransaction()
    '            Dim para(13) As SqlParameter

    '            para(0) = New SqlParameter("@ACTION", 2)
    '            para(1) = New SqlParameter("@Employee_Code", txtCode.Text)
    '            para(2) = New SqlParameter("@Department_ID", cboDepart.SelectedValue)
    '            para(3) = New SqlParameter("@Employee_Title", txtTitle.Text)
    '            para(4) = New SqlParameter("@Employee_Fname", txtFirstName.Text)
    '            para(5) = New SqlParameter("@Employee_Lname", txtLastName.Text)
    '            para(6) = New SqlParameter("@Employee_Position", txtPosition.Text)
    '            If chkSales.Checked Then
    '                para(7) = New SqlParameter("@Employee_Sale", 1)
    '            Else
    '                para(7) = New SqlParameter("@Employee_Sale", 0)
    '            End If
    '            para(8) = New SqlParameter("@Employee_StatusAccount", 0)
    '            para(9) = New SqlParameter("@Employee_level", 0)
    '            para(10) = New SqlParameter("@Employee_Status", index)
    '            para(11) = New SqlParameter("@Create_User", FRAMEWORK.Framework.F_Username_Str)
    '            para(12) = New SqlParameter("@Update_User", "")
    '            L_complete = DA.ExecuteNonQuery(DA.Transaction, CommandType.StoredProcedure, "sp_SYS_EMPLOYEE", para)
    '            If L_complete > 0 Then
    '                DA.CommitTransaction()
    '                MessageBox.Show(tmp & "ข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '                Call ClearData(Me, Me)
    '                Call Get_ALL_Employee()
    '            Else
    '                DA.RollBackTransaction()
    '                MessageBox.Show("เกิดข้อผิดพลาด", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '                Exit Sub
    '            End If

    '        Catch ex As Exception
    '            DA.RollBackTransaction()
    '            MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
    '            Exit Sub
    '        End Try

    '        cboDepart.Focus()

    '    Else
    '        Exit Sub
    '    End If
    'End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim lgb As New LinearGradientBrush(ClientRectangle, Color.LemonChiffon, Color.LightYellow, LinearGradientMode.Vertical)
        e.Graphics.FillRectangle(lgb, ClientRectangle)

        lgb.Dispose()
    End Sub

    'Private Sub GetDepartment()
    '    Dim dt As DataTable
    '    Dim para(8) As SqlParameter

    '    para(0) = New SqlParameter("@ACTION", 3)
    '    para(1) = New SqlParameter("@Department_ID", 0)
    '    para(2) = New SqlParameter("@Department_Name", "")
    '    para(3) = New SqlParameter("@Department_ShortName", "")
    '    para(4) = New SqlParameter("@Department_Status", 0)
    '    para(5) = New SqlParameter("@Department_Remark", "")
    '    para(6) = New SqlParameter("@Create_User", "")
    '    para(7) = New SqlParameter("@Update_User", "")
    '    dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_DEPARTMENT", para).Tables(0)

    '    If dt.Rows.Count > 0 Then
    '        cboDepart.DataSource = dt
    '        cboDepart.DisplayMember = "Department_Name"
    '        cboDepart.ValueMember = "Department_ID"

    '    Else
    '        cboDepart.DataSource = Nothing
    '    End If
    'End Sub
#End Region

    Private Sub frm_UserPermission_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        grbEmp.BackColor = Color.FromArgb(0, 0, 0, 0)
        GroupBox2.BackColor = Color.FromArgb(0, 0, 0, 0)
        GroupBox3.BackColor = Color.FromArgb(0, 0, 0, 0)
        Panel1.BackColor = Color.FromArgb(0, 0, 0, 0)

        Dim FD As New FormFader
        FD.FadeIn(Me, 0.25)
        Get_ALL_Employee()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Me.gbEmpCode.Visible = False Then
            Me.gbEmpCode.Visible = True

        Else
            Me.gbEmpCode.Visible = False
        End If
        chkBk.Checked = False
        chkOp.Checked = False
        chkReport.Checked = False
        chkSd.Checked = False
    End Sub
    Private Sub Get_ALL_Employee()
        Dim dt As DataTable

        Dim para(13) As SqlParameter

        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@Employee_Code", "")
        para(2) = New SqlParameter("@Department_ID", 0)
        para(3) = New SqlParameter("@Employee_Title", "")
        para(4) = New SqlParameter("@Employee_Fname", "")
        para(5) = New SqlParameter("@Employee_Lname", "")
        para(6) = New SqlParameter("@Employee_Position", "")
        para(7) = New SqlParameter("@Employee_Sale", 0)
        para(8) = New SqlParameter("@Employee_StatusAccount", 0)
        para(9) = New SqlParameter("@Employee_level", 0)
        para(10) = New SqlParameter("@Employee_Status", 0)
        para(11) = New SqlParameter("@Create_User", "")
        para(12) = New SqlParameter("@Update_User", "")
        dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_EMPLOYEE", para).Tables(0)

        If dt.Rows.Count > 0 Then
            dgvDetails.DataSource = dt
            FormatHeader()
        Else
            dgvDetails.DataSource = Nothing
        End If


    End Sub
    Private Sub FormatHeader()
        With dgvDetails
            If .RowCount > 0 Then
                .Columns(0).HeaderText = "รหัส"
                .Columns(1).HeaderText = "คำนำหน้า"
                .Columns(2).HeaderText = "ชื่อ"
                .Columns(3).HeaderText = "นามสกุล"
                .Columns(4).HeaderText = "หน้าที่/ตำแหน่ง"
                .Columns(5).HeaderText = "แผนก"
                .Columns(6).HeaderText = "Sales"
                .Columns(7).HeaderText = "User Account"
                .Columns(8).HeaderText = "Level"

                .Columns(0).Width = 100
                .Columns(1).Width = 80 '.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns(3).Width = 100
                .Columns(4).Width = 100
                .Columns(5).Visible = False
                .Columns(6).Visible = False
                .Columns(7).Visible = False
                .Columns(8).Visible = False
            End If
        End With
    End Sub

    Private Sub dgvDetails_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDetails.CellDoubleClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        Me.gbEmpCode.Visible = False
        ClearData(Me, Me)
        txtEmpcode.Text = dgvDetails.Rows(e.RowIndex).Cells(0).Value.ToString
        '*******************

        LoadUserName(txtEmpcode.Text)
        LoadEmployeeCode(txtEmpcode.Text)
    End Sub
    Private Sub ClearData(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
        Dim ctr As Control
        For Each ctr In parentCtr.Controls
            If Not (ctr Is ignoreControl) Then
                If TypeOf ctr Is TextBox Then
                    ctr.Text = ""
                ElseIf TypeOf ctr Is ComboBox Then
                    DirectCast(ctr, ComboBox).SelectedIndex = -1
                ElseIf TypeOf ctr Is ListBox Then
                    CType(ctr, ListBox).Items.Clear()
                End If
            End If
            ClearData(ctr, ignoreControl)
        Next
        dgvPerrmission.DataSource = Nothing
        txtUserName.Enabled = True
        txtUserName.ReadOnly = False
    End Sub
    Private Sub LoadEmployeeCode(ByVal empCode As String)
        Dim dt As DataTable
        dgvPerrmission.AutoGenerateColumns = False
        Dim para(1) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 1)
        para(1) = New SqlParameter("@User_ID", User_ID)

        dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_GetAllMenu", para).Tables(0)

        If dt.Rows.Count = 0 Then
            '****************
            Dim para2(1) As SqlParameter
            para2(0) = New SqlParameter("@ACTION", 0)
            para2(1) = New SqlParameter("@User_ID", User_ID)
            dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_GetAllMenu", para2).Tables(0)

        End If
        dgvPerrmission.DataSource = dt
    End Sub
    Private Sub LoadUserName(ByVal empCode As String)
        Dim dt As DataTable
        dgvPerrmission.AutoGenerateColumns = False
        Dim para(3) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 2)
        para(1) = New SqlParameter("@User_ID", 0)
        para(2) = New SqlParameter("@menuID", 0)
        para(3) = New SqlParameter("@Employee_Code", empCode)
        dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_UserPerrmission", para).Tables(0)
        If dt.Rows.Count <> 0 Then
            User_ID = CInt(dt.Rows(0)("User_ID").ToString())
            txtUserName.Text = dt.Rows(0)("User_UserName").ToString()
            txtPass1.Text = dt.Rows(0)("User_Password").ToString()
            txtPass2.Text = txtPass1.Text
            cboLevel.SelectedIndex = CInt(dt.Rows(0)("User_Level").ToString())
            txtUserName.ReadOnly = True
            txtUserName.Enabled = False
            btnEdit.Enabled = True
            btnAdd.Enabled = False
        Else
            User_ID = 0
            btnAdd.Enabled = True
            btnEdit.Enabled = False
            txtUserName.ReadOnly = False
            txtUserName.Enabled = True
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If txtUserName.Text = "" Then
            MessageBox.Show("กรุณาเลือกระบุ UserName Or Paasword !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUserName.Focus()
            Exit Sub
        End If

       
        If MessageBox.Show("คุณต้องการแก้ไขข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            EditUserName()
            SavePermission()
        Else
            Exit Sub
        End If
    End Sub
    Function AddUserName() As Integer

        Try
            Dim result As Integer
            DA.BeginTransaction()
            Dim para(4) As SqlParameter

            para(0) = New SqlParameter("@ACTION", 1)
            para(1) = New SqlParameter("@Employee_Code", txtEmpcode.Text)
            para(2) = New SqlParameter("@User_UserName", txtUserName.Text)
            para(3) = New SqlParameter("@User_Password", txtPass1.Text)
            para(4) = New SqlParameter("@User_Level", cboLevel.SelectedIndex)
            result = CInt(DA.ExecuteScalar(CommandType.StoredProcedure, "sp_Username", para).ToString())
            'If result = 1 Then
            DA.CommitTransaction()
            Return result

        Catch ex As Exception
            DA.RollBackTransaction()
            MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End Try



    End Function
    Private Sub EditUserName()

        Try
            Dim result As Integer
            DA.BeginTransaction()
            Dim para(4) As SqlParameter

            para(0) = New SqlParameter("@ACTION", 2)
            para(1) = New SqlParameter("@Employee_Code", txtEmpcode.Text)
            para(2) = New SqlParameter("@User_UserName", txtUserName.Text)
            para(3) = New SqlParameter("@User_Password", txtPass1.Text)
            para(4) = New SqlParameter("@User_Level", cboLevel.SelectedIndex)
            result = DA.ExecuteNonQuery(DA.Transaction, CommandType.StoredProcedure, "sp_Username", para)
            'If result = 1 Then
            DA.CommitTransaction()
            'MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ''Call ClearData(Me, Me)
            ''Call Get_ALL_Gram()
            'Else
            'DA.RollBackTransaction()
            'MessageBox.Show("เกิดข้อผิดพลาด", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
            'Exit Sub
            'End If

        Catch ex As Exception
            DA.RollBackTransaction()
            MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End Try



    End Sub
    Private Sub SavePermission()
        Try
            Dim result As Integer
            DA.BeginTransaction()
            Dim para(13) As SqlParameter
            Dim i As Integer
            For i = 0 To dgvPerrmission.Rows.Count - 1
                para(0) = New SqlParameter("@ACTION", 0)
                para(1) = New SqlParameter("@User_ID", User_ID)
                para(2) = New SqlParameter("@menuID", dgvPerrmission("colMenuCode", i).Value.ToString())
                para(3) = New SqlParameter("@menuName", dgvPerrmission("colManuName", i).Value.ToString())
                If dgvPerrmission("colIsview", i).Value.ToString() = "False" Or dgvPerrmission("colIsview", i).Value.ToString() = "0" Then
                    para(4) = New SqlParameter("@IsView", False)
                Else
                    para(4) = New SqlParameter("@IsView", True)
                End If
                If dgvPerrmission("colInsert", i).Value.ToString() = "False" Or dgvPerrmission("colInsert", i).Value.ToString() = "0" Then
                    para(5) = New SqlParameter("@IsInsert", False)
                Else
                    para(5) = New SqlParameter("@IsInsert", True)
                End If
                If dgvPerrmission("colEdit", i).Value.ToString() = "False" Or dgvPerrmission("colEdit", i).Value.ToString() = "0" Then
                    para(6) = New SqlParameter("@IsEdit", False)
                Else
                    para(6) = New SqlParameter("@IsEdit", True)
                End If
                If dgvPerrmission("colDel", i).Value.ToString() = "False" Or dgvPerrmission("colDel", i).Value.ToString() = "0" Then
                    para(7) = New SqlParameter("@IsDel", False)
                Else
                    para(7) = New SqlParameter("@IsDel", True)
                End If
                If dgvPerrmission("colPrint", i).Value.ToString() = "False" Or dgvPerrmission("colPrint", i).Value.ToString() = "0" Then
                    para(8) = New SqlParameter("@IsPrint", False)
                Else
                    para(8) = New SqlParameter("@IsPrint", True)
                End If
                If dgvPerrmission("colApprove", i).Value.ToString() = "False" Or dgvPerrmission("colApprove", i).Value.ToString() = "0" Then
                    para(9) = New SqlParameter("@Is_Approve", False)
                Else
                    para(9) = New SqlParameter("@Is_Approve", True)
                End If
                para(10) = New SqlParameter("@Create_User", FRAMEWORK.Framework.F_Username_Str)
                para(11) = New SqlParameter("@Create_Date", DateTime.Now())
                para(12) = New SqlParameter("@Update_User", FRAMEWORK.Framework.F_Username_Str)
                para(13) = New SqlParameter("@Update_Date", DateTime.Now())
                'FRAMEWORK.Framework.F_Username_Str

                result = DA.ExecuteNonQuery(DA.Transaction, CommandType.StoredProcedure, "sp_SaveMenu", para)
            Next

            If result = 1 Then
                DA.CommitTransaction()
                MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Call ClearData(Me, Me)
                'LoadUserName(txtEmpcode.Text)
                'LoadEmployeeCode(txtEmpcode.Text)
            Else
                DA.RollBackTransaction()
                MessageBox.Show("เกิดข้อผิดพลาด", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

        Catch ex As Exception
            DA.RollBackTransaction()
            MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End Try
    End Sub
    Private Sub dgvDetails_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDetails.CellContentClick

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ClearData(Me, Me)
        frm_UserPermission_Load(sender, e)
    End Sub

    Private Sub chkSd_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSd.CheckedChanged
        Dim i As Integer = 0
        If chkSd.Checked Then


            If cboLevel.SelectedIndex = 0 Then ' ระดับต่ำสุด จะมีแค่ view เท่านั้น 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0001" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0016" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                        dgvPerrmission.Rows(i).Cells("colDel").Value = False
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                    End If
                Next
            ElseIf cboLevel.SelectedIndex = 1 Then 'ผู้ใช้ระดับกลาง, ทำรายการ order ได้หมด 

                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0001" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0016" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = True
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = True
                        dgvPerrmission.Rows(i).Cells("colDel").Value = True
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = True
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = True
                    End If
                Next
            Else
                'ระดับสูง ทำรายการได้หมด เหมือนกัน เขียนแยกกัน เผื่อไว้ 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0001" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0016" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = True
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = True
                        dgvPerrmission.Rows(i).Cells("colDel").Value = True
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = True
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = True
                    End If
                Next
            End If
        Else
            For i = 0 To dgvPerrmission.Rows.Count - 1
                If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0001" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0016" Then
                    dgvPerrmission.Rows(i).Cells("colIsview").Value = False
                    dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                    dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                    dgvPerrmission.Rows(i).Cells("colDel").Value = False
                    dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                    dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                End If
            Next
        End If


    End Sub

    Private Sub chkOp_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOp.CheckedChanged
        Dim i As Integer = 0
        If chkOp.Checked Then ' config เห็นเฉพาะระดับสูงเท่านั้น 


            If cboLevel.SelectedIndex = 0 Then ' ระดับต่ำสุด จะมีแค่ view เท่านั้น 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0002" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0003" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0004" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0005" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0006" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0007" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0008" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0009" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0010" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0011" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0012" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0013" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0014" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0015" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = False
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                        dgvPerrmission.Rows(i).Cells("colDel").Value = False
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                    End If
                Next
            ElseIf cboLevel.SelectedIndex = 1 Then 'ผู้ใช้ระดับกลาง, ทำรายการ order ได้หมด 

                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0002" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0003" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0004" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0005" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0006" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0007" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0008" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0009" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0010" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0011" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0012" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0013" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0014" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0015" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = False
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                        dgvPerrmission.Rows(i).Cells("colDel").Value = False
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                    End If
                Next
            Else
                'ระดับสูง ทำรายการได้หมด เหมือนกัน เขียนแยกกัน เผื่อไว้ 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0002" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0003" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0004" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0005" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0006" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0007" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0008" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0009" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0010" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0011" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0012" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0013" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0014" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0015" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = True
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = True
                        dgvPerrmission.Rows(i).Cells("colDel").Value = True
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = True
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = True
                    End If
                Next
            End If
        Else
            For i = 0 To dgvPerrmission.Rows.Count - 1
                If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0002" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0003" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0004" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0005" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0006" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0007" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0008" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0009" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0010" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0011" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0012" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0013" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0014" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0015" Then
                    dgvPerrmission.Rows(i).Cells("colIsview").Value = False
                    dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                    dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                    dgvPerrmission.Rows(i).Cells("colDel").Value = False
                    dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                    dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                End If
            Next
        End If
    End Sub

    Private Sub chkReport_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkReport.CheckedChanged
        Dim i As Integer = 0
        If chkReport.Checked Then


            If cboLevel.SelectedIndex = 0 Then ' ระดับต่ำสุด จะมีแค่ view เท่านั้น 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0018" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0019" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                        dgvPerrmission.Rows(i).Cells("colDel").Value = False
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                    End If
                Next
            ElseIf cboLevel.SelectedIndex = 1 Then 'ผู้ใช้ระดับกลาง, ทำรายการ order ได้หมด 

                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0018" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0019" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                        dgvPerrmission.Rows(i).Cells("colDel").Value = False
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                    End If
                Next
            Else
                'ระดับสูง ทำรายการได้หมด เหมือนกัน เขียนแยกกัน เผื่อไว้ 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0018" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0019" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = True
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = True
                        dgvPerrmission.Rows(i).Cells("colDel").Value = True
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = True
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = True
                    End If
                Next
            End If
        Else
            For i = 0 To dgvPerrmission.Rows.Count - 1
                If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0018" Or dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0019" Then
                    dgvPerrmission.Rows(i).Cells("colIsview").Value = False
                    dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                    dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                    dgvPerrmission.Rows(i).Cells("colDel").Value = False
                    dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                    dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                End If
            Next
        End If
    End Sub

    Private Sub cboLevel_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLevel.SelectedIndexChanged
        chkBk.Checked = False
        chkOp.Checked = False
        chkReport.Checked = False
        chkSd.Checked = False
        chkBk.Checked = True
        chkOp.Checked = True
        chkReport.Checked = True
        chkSd.Checked = True
      
    End Sub


    Private Sub chkBk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBk.CheckedChanged
        If chkBk.Checked Then


            If cboLevel.SelectedIndex = 0 Then ' ระดับต่ำสุด จะมีแค่ view เท่านั้น 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0017" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = True
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = True
                        dgvPerrmission.Rows(i).Cells("colDel").Value = True
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = True
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = True
                    End If
                Next
            ElseIf cboLevel.SelectedIndex = 1 Then 'ผู้ใช้ระดับกลาง, ทำรายการ order ได้หมด 

                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0017" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = True
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = True
                        dgvPerrmission.Rows(i).Cells("colDel").Value = True
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = True
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = True
                    End If
                Next
            Else
                'ระดับสูง ทำรายการได้หมด เหมือนกัน เขียนแยกกัน เผื่อไว้ 
                For i = 0 To dgvPerrmission.Rows.Count - 1
                    If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0017" Then
                        dgvPerrmission.Rows(i).Cells("colIsview").Value = True
                        dgvPerrmission.Rows(i).Cells("colInsert").Value = True
                        dgvPerrmission.Rows(i).Cells("colEdit").Value = True
                        dgvPerrmission.Rows(i).Cells("colDel").Value = True
                        dgvPerrmission.Rows(i).Cells("colPrint").Value = True
                        dgvPerrmission.Rows(i).Cells("colApprove").Value = True
                    End If
                Next
            End If
        Else
            For i = 0 To dgvPerrmission.Rows.Count - 1
                If dgvPerrmission.Rows(i).Cells("colMenuCode").Value.ToString() = "0017" Then
                    dgvPerrmission.Rows(i).Cells("colIsview").Value = False
                    dgvPerrmission.Rows(i).Cells("colInsert").Value = False
                    dgvPerrmission.Rows(i).Cells("colEdit").Value = False
                    dgvPerrmission.Rows(i).Cells("colDel").Value = False
                    dgvPerrmission.Rows(i).Cells("colPrint").Value = False
                    dgvPerrmission.Rows(i).Cells("colApprove").Value = False
                End If
            Next
        End If
    End Sub
    Function CheckUserName() As Integer
        Dim result As Integer
        Dim par(4) As SqlParameter
        Dim dt As DataTable
        par(0) = New SqlParameter("@ACTION", 3)
        par(1) = New SqlParameter("@Employee_Code", txtEmpcode.Text)
        par(2) = New SqlParameter("@User_UserName", txtUserName.Text)
        par(3) = New SqlParameter("@User_Password", txtPass1.Text)
        par(4) = New SqlParameter("@User_Level", cboLevel.SelectedIndex)
        result = CInt(DA.ExecuteScalar(CommandType.StoredProcedure, "sp_Username", par).ToString())
        Return result

    End Function

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If cboLevel.SelectedIndex = -1 Then
            MessageBox.Show("กรุณาเลือกระดับพนักงาน ", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If txtUserName.Text = "" Or txtPass1.Text <> txtPass2.Text Then
            MessageBox.Show("กรุณาระบุ Username หรือ Password ไม่ถูกต้อง ", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUserName.SelectAll()
            txtUserName.Focus()
            Exit Sub
        End If
        If CheckUserName() = 0 Then
            MessageBox.Show("Username นี้ มีผู้ใช้งานแล้ว ", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtUserName.SelectAll()
            txtUserName.Focus()
            Exit Sub
        End If
        If MessageBox.Show("คุณต้องการเพิ่มข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            User_ID = AddUserName()
            SavePermission()
        Else
            Exit Sub
        End If
    End Sub

    Private Sub txtSaarch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dt As DataTable
        dgvPerrmission.AutoGenerateColumns = False
        Dim para(1) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 1)
        para(1) = New SqlParameter("@User_ID", User_ID)

        dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_GetAllMenu", para).Tables(0)

        If dt.Rows.Count = 0 Then
            '****************
            Dim para2(1) As SqlParameter
            para2(0) = New SqlParameter("@ACTION", 0)
            para2(1) = New SqlParameter("@User_ID", User_ID)
            dt = DA.ExecuteDataset(CommandType.StoredProcedure, "sp_GetAllMenu", para2).Tables(0)

        End If
        dgvPerrmission.DataSource = dt
    End Sub
End Class